import {expect} from 'chai'
import {describe} from 'mocha'
import * as authUtil from '../src/auth-helper'

describe('测试 getHttpPolicy 方法', () => {
    it('仓库地址以https开头', () => {
        let gitRepo = 'https://gitee.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo, "")
        expect(result).equal('https')
    })

    it('仓库地址以http开头,且是aliyun', () => {
        let gitRepo = 'http://aliyun.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"aliyun")
        expect(result).equal('https')
    })

    it('仓库地址以http开头,gitee', () => {
        let gitRepo = 'http://gitee.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"gitee")
        expect(result).equal('https')
    })
    it('仓库地址以http开头,bitbucket', () => {
        let gitRepo = 'http://bitbucket.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"bitbucket")
        expect(result).equal('https')
    })
    it('仓库地址以http开头,且是git', () => {
        let gitRepo = 'http://git.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"git")
        expect(result).equal('http')
    })

    it('仓库地址以http开头,gitlab', () => {
        let gitRepo = 'http://gitlab.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"gitlab")
        expect(result).equal('http')
    })
    it('仓库地址以http开头,customGitlab', () => {
        let gitRepo = 'http://aliyun.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"customGitlabtn")
        expect(result).equal('http')
    })
    it('仓库地址以http开头,svn', () => {
        let gitRepo = 'http://svn.com/leeyebin/course.git'
        let result = authUtil.getHttpPolicy(gitRepo,"svn")
        expect(result).equal('http')
    })

})

describe('测试 getGitEndpoint 方法', () => {
    it('仓库地址以http开头', () => {
        let gitRepo = 'http://gitee.com/leeyebin/course.git'
        let result = authUtil.getGitEndpoint(gitRepo)
        expect(result).equal('gitee.com/leeyebin/course.git')
    })

    it('仓库地址以https开头', () => {
        let gitRepo = 'https://gitee.com/leeyebin/course.git'
        let result = authUtil.getGitEndpoint(gitRepo)
        expect(result).equal('gitee.com/leeyebin/course.git')
    })

    it('仓库地址以git开头', () => {
        let gitRepo = 'git@gitee.com:leeyebin/course.git'
        let result = authUtil.getGitEndpoint(gitRepo)
        expect(result).equal('gitee.com/leeyebin/course.git')
    })
})


describe('测试 getCloneUrl 方法', () => {
    it('http协议', () => {
        let user = 'admin@daf'
        let password = 'admin'
        let gitRepo = 'https://gitee.com/leeyebin/course.git'
        let method = 'http'
        let gitEndpint = 'gitee.com/leeyebin/course.git'
        let result = authUtil.getCloneUrl(
            user,
            password,
            gitRepo,
            method,
            'https',
            gitEndpint
        )
        expect(result).equal(
            'https://admin%40daf:admin@gitee.com/leeyebin/course.git'
        )
    })

    it('ssh协议', () => {
        let user = 'admin'
        let password = 'admin'
        let gitRepo = 'https://gitee.com/leeyebin/course.git'
        let method = 'ssh'
        let gitEndpint = 'gitee.com/leeyebin/course.git'
        let result = authUtil.getCloneUrl(
            user,
            password,
            gitRepo,
            method,
            'https',
            gitEndpint
        )
        expect(result).equal('git@gitee.com:leeyebin/course.git')
    })
})

describe('测试 getHost 方法', () => {
    it('仓库地址以https开头', () => {
        let url = 'https://gitee.com/leeyebin/course.git'
        let result = authUtil.getHost(url)
        expect(result).equal('gitee.com')
    })

    it('仓库地址以http开头', () => {
        let url = 'http://gitee.com/leeyebin/course.git'
        let result = authUtil.getHost(url)
        expect(result).equal('gitee.com')
    })

    it('仓库地址以git开头', () => {
        let url = 'git@gitee.com:leeyebin/course.git'
        let result = authUtil.getHost(url)
        expect(result).equal('gitee.com')
    })
})

describe('测试 getUser 方法', () => {
    const originalEnv = process.env;

    beforeEach(() => {
        process.env = { ...originalEnv };
    });

    afterEach(() => {
        process.env = originalEnv;
    });

    it('should throw an error when type is undefined', () => {
        expect(() => authUtil.getUser(undefined, 'hostGitUserName')).throw('resolve repo type failed');
    });

    it('should return GIT_USERNAME when type is aliyun and GIT_USERNAME is defined', () => {
        process.env['GIT_USERNAME'] = 'aliyunUser';
        expect(authUtil.getUser('aliyun', 'hostGitUserName')).equal('aliyunUser');
    });

    it('should return an empty string when type is aliyun and GIT_USERNAME is undefined', () => {
        delete process.env['GIT_USERNAME'];
        expect(authUtil.getUser('aliyun', 'hostGitUserName')).equal('');
    });

    it('should return GITEE_USERNAME when type is gitee and GITEE_USERNAME is defined', () => {
        process.env['GITEE_USERNAME'] = 'giteeUser';
        expect(authUtil.getUser('gitee', 'hostGitUserName')).equal('giteeUser');
    });

    it('should return an empty string when type is gitee and GITEE_USERNAME is undefined', () => {
        delete process.env['GITEE_USERNAME'];
        expect(authUtil.getUser('gitee', 'hostGitUserName')).equal('');
    });

    it('should return hostGitUserName when type is git', () => {
        expect(authUtil.getUser('git', 'hostGitUserName')).equal('hostGitUserName');
    });

    it('should return hostGitUserName when type is customGitlab', () => {
        expect(authUtil.getUser('customGitlab', 'hostGitUserName')).equal('hostGitUserName');
    });

    it('should return hostGitUserName for unknown type', () => {
        expect(authUtil.getUser('unknownType', 'hostGitUserName')).equal('hostGitUserName');
    });
})

describe('测试 getPassword 方法', () => {
    it('should throw an error when type is undefined', () => {
        expect(() => authUtil.getPassword(undefined, 'password')).throw('resolve repo type failed');
    });

    it('should return GIT_PRIVATE_TOKEN if defined', () => {
        process.env['GIT_PRIVATE_TOKEN'] = 'aliyunToken';
        expect(authUtil.getPassword('aliyun', 'password')).equal('aliyunToken');
        delete process.env['GIT_PRIVATE_TOKEN'];
    });

    it('should return an empty string if GIT_PRIVATE_TOKEN is not defined', () => {
        expect(authUtil.getPassword('aliyun', 'password')).equal('');
    });

    it('should return GITEE_ACCESS_TOKEN if defined', () => {
        process.env['GITEE_ACCESS_TOKEN'] = 'giteeToken';
        expect(authUtil.getPassword('gitee', 'password')).equal('giteeToken');
        delete process.env['GITEE_ACCESS_TOKEN'];
    });

    it('should return an empty string if GITEE_ACCESS_TOKEN is not defined', () => {
        expect(authUtil.getPassword('gitee', 'password')).equal('');
    });

    it('should return hostGitPassword', () => {
        expect(authUtil.getPassword('git', 'gitPassword')).equal('gitPassword');
    });

    it('should return hostGitPassword', () => {
        expect(authUtil.getPassword('customGitlab', 'customGitlabPassword')).equal('customGitlabPassword');
    });

    it('should return hostGitPassword', () => {
        expect(authUtil.getPassword('other', 'otherPassword')).equal('otherPassword');
    });
})


